<?php
/* @var $this SiteController */
//
//$this->pageTitle=Yii::app()->name . ' - About';
//$this->breadcrumbs=array(
//	'About',
//);
?>


<div id="excel-wrapper" class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-primary">
                <div class="panel-heading">项目排期</div>
                <div class="panel-body">
                    <div id="excel-container" style="overflow: scroll">

                    </div>
                </div>
            </div>

        </div>

    </div>
</div>
<script>
    $(function() {
       
        loadContent();
//alert(data);
        // buildExcel();

        var sheetContent  //数据内容
                , $container = $("#excel-container")
                , handsontable = $container.data('handsontable');
        //        , blankContent = handsontable.getData();

//        loadContent();

        //handsontable.alter('insert_col', 2,2);
        //  handsontable.alter('remove_col', 2,2);

        //加载表格内容



    });
    function loadContent() {

        $.ajax({
            type: "post",
            url: "/testdrive/index.php/schedule/schedulelist",
            data: "",
            success: function(str) {
                // alert(str);
                object = JSON.parse(str);
                testbuildExcel(object);

                //    handsontable.loadData(object);
                //     var ht = $('#excel-container').data('handsontable');
                //ht.setDataAtCell(0, 0, 'new value');

                // handsontable.alter('insert_col', null, 1, 'colHeaders:"任务六"');

            },
            error: function() {
                alert("不能获得值");

            }
        }

        );

    }

    function buildExcel() {
        var data = [
            {dateline: "", one: "", two: "", three: "", four: "", five: ""}
        ];
        // var  colHeader=["日期", "任务一", "任务二", "任务三", "任务四", "任务五"],

        var $tmpExcel = $('#excel-container');
        $tmpExcel.handsontable({
            data: data,
            minSpareRows: 1,
            //   colHeaders: ["日期", "任务一", "任务二", "任务三", "任务四", "任务五"],
//                        colHeaders:colHeader,
            manualColumnResize: true,
            manualRowResize: true,
            fixedRowsTop: 1,
            fixedColumnsLeft: 1,
            //      contextMenu: true,
            columnSorting: true,
            columns: [
                {
                    data: "dateline",
                },
                {
                    data: "one"
                },
                {
                    data: "two"
                },
                {
                    data: "three"
                },
                {
                    data: "four"
                },
                {
                    data: "five",
                }

            ]


        });
    }


    function firstRowRenderer(instance, td, row, col, prop, value, cellProperties) {
        Handsontable.renderers.TextRenderer.apply(this, arguments);
        td.style.fontWeight = 'bold';
        td.style.color = '#2d3e50';
        td.style.background = '#CEC';
        //  td.style.background = '#2d3e50';

    }

    function negativeValueRenderer(instance, td, row, col, prop, value, cellProperties) {
        Handsontable.renderers.TextRenderer.apply(this, arguments);
        if (parseInt(value, 10) < 0) { //if row contains negative number
            td.className = 'negative'; //add class "negative"
        }

        if (!value || value === '') {
            td.style.background = '#EEE';
        }
        else {
            if (value === 'Nissan') {
                td.style.fontStyle = 'italic';
            }
            td.style.background = '';
        }
    }

//    var data = [
//        ["", "Kia", "Nissan", "Toyota", "Honda"],
//        ["2008", -5, '', 12, 13],
//        ["2009", '', -11, 14, 13],
//        ["2010", '', 15, -12, "readOnly"]
//    ];




    function testbuildExcel(data) {
        Handsontable.renderers.registerRenderer('negativeValueRenderer', negativeValueRenderer); //maps function to lookup string

        var container = $("#excel-container");
        container.handsontable({
            data: data,
            startRows: 5,
            startCols: 5,
            minSpareRows: 1,
            contextMenu: true,
//            onSelection: function(row, col, row2, col2) {
//                var meta = container.handsontable('getCellMeta', row2, col2);
//                if (meta.readOnly) {
//                    container.handsontable('updateSettings', {fillHandle: false});
//                }
//                else {
//                    container.handsontable('updateSettings', {fillHandle: true});
//                }
//            },
            cells: function(row, col, prop) {
                var cellProperties = {};
                if (row === 0 || container.handsontable('getData')[row][col] === 'readOnly') {
                    cellProperties.readOnly = true; //make cell read-only if it is first row or the text reads 'readOnly'
                }
                if (row === 0) {
                    cellProperties.renderer = firstRowRenderer; //uses function directly
                }
                if (col === 0) {
                    cellProperties.renderer = firstRowRenderer; //uses function directly
                }
//
//            else {
//                cellProperties.renderer = "negativeValueRenderer"; //uses lookup map
//            }
                return cellProperties;
            }
        });
    }

</script>
